<?php

namespace App\Admin\Controllers\user;

use App\Common\statement\CommonStatement;
use App\Models\user\Address;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class AddressController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = 'App\Models\user\Address';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Address);
        $grid->model()->orderByDesc('id');
        $grid->filter(function($filter){
            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            //展开搜索
//            $filter->expand();
            $filter->column(1/3, function ($filter) {

//                $filter->equal( 'id',__('Id'));
                $filter->where(function ($query) {

                    $query->where('user.uuid', 'like', "%{$this->input}%")
                        ->orWhere('user_id', 'like', "%{$this->input}%");

                },__('Uuid'));

//                $filter->like( 'user.phone',__('Phone'));

            });
            $filter->column(1/3, function ($filter) {
                $filter->like( 'user.phone',__('Phone'));
            });
            $filter->column(1/3, function ($filter) {
                $filter->between('created_at',__('Created at'))->datetime();
            });

        });
        $grid->column('id', __('Id'));
        $grid->column('user_id', __('User id'));
        $grid->column('user.header_img', __('header_img'))
            ->lightbox(['width' => 50, 'height' => 50, 'class' => ['circle', 'thumbnail']]);

        $grid->column('user.name', __('Name'));
        $grid->column('user.phone', __('Phone'));
        $grid->column('address', __('地址'))->display(function ($s){
            return sprintf("%s %s %s",$this->p_name,$this->c_name,$this->d_name);
        });
        $grid->column('detail', __('Detail'));
        $grid->column('phone', __('Phone'));
        $grid->column('is_default', __('Is default'))->switch(CommonStatement::default_switch);
        $grid->column('created_at', __('Created at'));
        $grid->column('updated_at', __('Updated at'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Address::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('user_id', __('User id'));
        $show->field('detail', __('Detail'));
        $show->field('phone', __('Phone'));
        $show->field('is_default', __('Is default'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Address);

        $form->number('user_id', __('User id'))->readonly();
        $form->text('detail', __('Detail'));
        $form->mobile('phone', __('Phone'));
        $form->switch('is_default', __('Is default'))->states(CommonStatement::default_switch);

        return $form;
    }
}
